 
import { RoleBehavior } from "utils/constants/bodyconstant";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
import { BodyUtils } from "utils/creep/bodyutils";
import { role_crossLevel } from "utils/constants/moveConstant";
import { spawn } from "child_process";

export class ActionSpawn  extends ActionBase<IActionCMD<"spawn">>
{
    execute(action: IActionCMD<"spawn">): ActionCode 
    {
       
        const arg = action.arg;
        const entity = arg.entity;
        const Spawn = Game.spawns[arg.spawnName];
        const bodyconfig  = action.arg.bodycfg;
        //global.Log.Warning(`ActionSpawn 执行孵化任务! ${JSON.stringify(bodyconfig)}`);
        // entity.log('info',"准备孵化",`${JSON.stringify(bodyconfig)}`);
        let bodyParts = BodyUtils.flatBodyParts(bodyconfig.body);
        let needEnergy = BodyUtils.calculateSpawnEnergy(bodyParts);
        if (!bodyconfig.isAdapt && needEnergy > arg.entity.room.energyCapacityAvailable) {
            // 超出孵化能量上限
            entity.log("info",`孵化失败:${JSON.stringify(bodyParts)}`);
            return  -2;
        }
        
        if(bodyconfig.isAdapt)
        {
            if(Spawn.room.isEnergyPoor())
            {
                bodyParts = BodyUtils.adaptiveBody(bodyParts,Math.max(arg.entity.room.energyAvailable,300 ));
                needEnergy = BodyUtils.calculateSpawnEnergy(bodyParts);

                if(needEnergy>300)
                {
                    entity.log('info',arg.creepName,"孵化错误,低能量模式下当前能量不足以孵化",`${JSON.stringify(bodyParts)}`,needEnergy);
                }
                
            }else if(needEnergy> arg.entity.room.energyCapacityAvailable )
            {
                 // 正常能量 ->arg.entity.room.energyCapacityAvailable
                // 低能量模式 -> Math.max(arg.entity.room.energyAvailable,300 )
                bodyParts = BodyUtils.adaptiveBody(bodyParts,arg.entity.room.energyCapacityAvailable);
                needEnergy = BodyUtils.calculateSpawnEnergy(bodyParts)
                
            }

        }

        let roomEnergy = Spawn.room.energyAvailable;
        // || bodyconfig.isAdapt
       
        if (!Spawn.spawning && (roomEnergy >= needEnergy ))
        {
            // global.Log.Info(`directions  ${JSON.stringify(entity.getSpawnDirections(Spawn))} ${needEnergy}<=${roomEnergy}`)
            // global.Log.Info(`bodys  ${JSON.stringify(bodyParts)}`)
            
            const bodymap:Record<BodyPartConstant,number>={} as any;
            bodyParts.forEach(e=>
                {
                    if(bodymap[e])
                    {
                        bodymap[e]++;
                    }
                    else
                    {
                        bodymap[e]=1
                    }
                })
         
            let RetCode = Spawn.spawnCreep(bodyParts, arg.creepName, {directions:entity.getSpawnDirections(Spawn), memory: 
                {
                    Role: bodyconfig.Role ,
                    BoostLevel:bodyconfig.boost?bodyconfig.boost:0,
                    crossLevel:arg.corssLevel?arg.corssLevel:role_crossLevel[bodyconfig.Role],
                    OwnerRoom:arg.roomName,
                    Debug:{},
                     
                    // behaviorMask:RoleBehavior[bodyconfig.Role],
                    msk:RoleBehavior[bodyconfig.Role],
                    body:bodymap,
                    teamid:arg.teamid
                },
                 });
            if(RetCode == OK)
            {
                entity.room.energyAvailable-=needEnergy;
                // 孵化成功
             
                return 0;
            }
            else
            {
                entity.log("info",`孵化错误:  ${RetCode} parts ${JSON.stringify(bodyParts)}`)
                return -2;
            }
        }
      
        return 0;
    }
}